<?php
final class Wephp
{
    
    public static function run(){
          self::_set_const();//定义常量
          defined('DEBUG') || define('DEBUG',false);//用户不写开启DEBUG  则默认为不开启
          if(DEBUG){
            self::_create_dir();
            self::_import_file();
          } else {
            error_reporting(0);
            self::_import_file();
            //require RUNLOG_PATH . '/~boot.php'; //如果项目上线，关闭DEBUG模式，就不用执行以上两个函数，直接引入已经生成好的 boot.php
          }
          // self::_create_dir();//创建核心文件夹
          // self::_import_file();//引入调用核心文件
          Application::run();//框架核心类  位置：wephp->Lib->Core->Application.class.php,下面函数已经引入此类
    }

    private static function _set_const(){
       // var_dump(__FILE__);  输出D:\phpStudy\WWW\wephp\wephp\wephp.php 
        define('WEPHP_VERSION','1.0.0');//框架版本
        $path = str_replace('\\', '/', __FILE__); //替换__FILE__中的‘\\’,linux中是'\\'   
        define('WEPHP_PATH', dirname($path));//不带文件名，只有路径(框架根目录)
        //echo WEPHP_PATH;
        define('CONFIG_PATH', WEPHP_PATH . '/Config');//框架配置项目录
        define('DATA_PATH', WEPHP_PATH . '/Data');
        define('LIB_PATH', WEPHP_PATH . '/Lib');
        define('CORE_PATH', LIB_PATH . '/Core');
        define('FUNCTION_PATH', LIB_PATH . '/Function');
        define('ROOT_PATH', dirname(WEPHP_PATH));//应用目录
        define('RUNLOG_PATH', ROOT_PATH . '/Runlog');//临时目录
        define('LOG_PATH', RUNLOG_PATH . '/log');//日志目录
        define('APP_COMPILE_PATH', RUNLOG_PATH . '/' . APP_NAME . '/Compile');
        define('APP_CACHE_PATH',  RUNLOG_PATH . '/' . APP_NAME .'/Cache');
        define('EXTNEDS_PATH', WEPHP_PATH. '/Extends');
        define('ORG_PATH', EXTNEDS_PATH . '/Org');
        define('TOOL_PATH', EXTNEDS_PATH . '/Tool');
        //echo ROOT_PATH;
        define('APP_PATH', ROOT_PATH . '/' . APP_NAME);//
        //echo APP_PATH;
        define('APP_STATIC_PATH', ROOT_PATH. '/Public' . '/static');
        define('COMMON_PATH', ROOT_PATH . '/Common');
        define('COMMON_CONFIG_PATH', ROOT_PATH . '/Common' . '/Config');
        define('COMMON_MODEL_PATH', COMMON_PATH . '/Model');
        define('COMMON_LIB_PATH',COMMON_PATH . '/Lib' );
        
        define('APP_CONFIG_PATH', APP_PATH . '/' . 'Config');
        define('APP_CONTROLLER_PATH', APP_PATH . '/' . 'Controller');
        define('APP_VIEW_PATH', APP_PATH . '/' . 'View');
        define('APP_PUBLIC_PATH', APP_VIEW_PATH . '/' . 'Public');
        define('APP_INDEX_PATH', APP_VIEW_PATH . '/' . 'Index');
         
        define('IS_POST',($_SERVER['REQUEST_METHOD'] == 'POST') ? true : false);
        if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest') {
          define('IS_AJAX', true);
        } else {
          define('IS_AJAX', false);
        }
    }

    private static function _create_dir(){ //创建应用目录文件夹
        $app_dir = array(   //需要创建的目录路径数组
            EXTNEDS_PATH,
            TOOL_PATH,
            ORG_PATH,
            COMMON_CONFIG_PATH,
            COMMON_MODEL_PATH,
            COMMON_LIB_PATH,
            COMMON_PATH,
            RUNLOG_PATH,
            LOG_PATH, 
            APP_COMPILE_PATH,
            APP_CACHE_PATH,
            APP_PATH,
            APP_CONFIG_PATH,
            APP_CONTROLLER_PATH,
            APP_VIEW_PATH,
            APP_PUBLIC_PATH,
            APP_INDEX_PATH,
            APP_STATIC_PATH,              
        	); 
        foreach($app_dir as $v){ //创建应用目录
        	is_dir($v) || mkdir($v,0777,true);
        }	
        is_file(APP_VIEW_PATH . '/success.html') || copy(DATA_PATH . '/Tpl/success.html', APP_VIEW_PATH . '/success.html');
        is_file(APP_VIEW_PATH . '/error.html') || copy(DATA_PATH . '/Tpl/error.html', APP_VIEW_PATH . '/error.html');   
    }

    private static function _import_file(){ //载入所需文件
         $function_flie = array(
             CORE_PATH . '/Log.we.php',
             FUNCTION_PATH . '/function.we.php',
             ORG_PATH   . '/Smarty/Smarty.class.php',
             CORE_PATH  . '/SmartyWe.we.php',
             CORE_PATH . '/WephpController.we.php',
             CORE_PATH . '/Application.we.php'
         );
         
         $str = '';
         foreach ($function_flie as $v) {
               		$str .= trim(substr(file_get_contents($v),5, -2));
                  require_once $v;
               	}

                $str = "<?php\r\n" . $str;
                file_put_contents(RUNLOG_PATH . '/~boot.php', $str) || die('access not allow');    	
    }
}

Wephp::run();

?>